package dbms.parse;

import java.io.FileInputStream;

import dbms.Debug;
import dbms.absyn.AlterExp;
import dbms.absyn.CheckExp;
import dbms.absyn.CreateDbClause;
import dbms.absyn.CreateTabClause;
import dbms.absyn.DeleteExp;
import dbms.absyn.DropExp;
import dbms.absyn.DropTabClause;
import dbms.absyn.DropViewClause;
import dbms.absyn.Exp;
import dbms.absyn.ExpList;
import dbms.absyn.IndexExp;
import dbms.absyn.InsertExp;
import dbms.absyn.JoinExp;
import dbms.absyn.SelectExp;
import dbms.absyn.UnionAllExp;
import dbms.absyn.UnionExp;
import dbms.absyn.UpdateExp;
import dbms.absyn.ViewExp;
import dbms.planner.QueryPlanner;
import dbms.planner.UpdatePlanner;
import dbms.query.PlanTree;
import dbms.server.DbmsServer;

public class test_parser {

	public static void main(String argv[]) throws Exception {
		
		
		DbmsServer.init("test1");
		
		String testFile = "testcase/huge_data1.txt";
		FileInputStream fis = new FileInputStream(testFile);
		
		Lexer lexer = new Lexer(fis);
		parser p = new parser(lexer);

		ExpList expList = (ExpList)p.parse().value;
		
		long start = System.currentTimeMillis();
		executeDbms(expList);
		Debug.P((System.currentTimeMillis() - start) / 1000);
		
		fis.close();
		
		
		DbmsServer.shutdown();
	}
	
	public static String  executeDbms(ExpList sqlList) throws InterruptedException{
		int count = 0;
		while(sqlList != null){
			Exp sql = sqlList.exp;
			if(sql instanceof SelectExp){
				//SELECT ... FROM table WHERE ...
				//return executeQuery((SelectExp)sql);
				Debug.P(executeQuery((SelectExp)sql));
			} else if(sql instanceof InsertExp){
				//INSERT INTO table VALUES(...)
				UpdatePlanner.executeInsert((InsertExp)sql);
			} else if(sql instanceof UpdateExp){
				//UPDATE table SET ... WHERE ...
				UpdatePlanner.executeUpdate((UpdateExp)sql);
			} else if(sql instanceof DeleteExp){
				//DELETE FROM table WHERE ...
				UpdatePlanner.executeDelete((DeleteExp)sql);
			} else if(sql instanceof JoinExp){
				
			} else if(sql instanceof CreateDbClause){
				
			} else if(sql instanceof CreateTabClause){
				//CREATE TABLE table(id INT, name CHAR(10));
				UpdatePlanner.executeCreateTable((CreateTabClause)sql);
			} else if(sql instanceof UnionExp){
				
			} else if(sql instanceof UnionAllExp){
				
			} else if(sql instanceof AlterExp){
				
			} else if(sql instanceof CheckExp){
				
			} else if(sql instanceof IndexExp){
				
			} else if(sql instanceof DropExp){
				if(((DropExp)sql).drop_stmt instanceof DropTabClause){
					//DROP TABLE table;
					UpdatePlanner.executeDropTable(((DropTabClause)((DropExp)sql).drop_stmt));
				} else if(((DropExp)sql).drop_stmt instanceof DropViewClause){
					UpdatePlanner.executeDropView(((DropViewClause)((DropExp)sql).drop_stmt));
				} else {
					
				}
			} else if(sql instanceof ViewExp){
				UpdatePlanner.executeCreateView("create view vtest as select * from test;", (ViewExp)sql);
			} else{
				Debug.P("Illegal sql statement.");
			}
			count++;
			Debug.P(count + "**");
			sqlList = sqlList.next;
			Thread.sleep(10);
		}
		return null;
	}
	
	public static String executeQuery(SelectExp sql){
		String table = sql.from_stmt.tableList.attr.attr.name;
		if(DbmsServer.masterPage.viewMap.containsKey(table)){
			return QueryPlanner.executeQuery(sql);
		} else {
			PlanTree pTree = new PlanTree();
			pTree.query(sql);
			pTree.print();
			return pTree.execute().resultSet.toString();
		}
		
	}
}